{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Minimum Distance to the Target Element"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: getMinDistance"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #到目标元素的最小距离"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组 <code>nums</code> （下标 <strong>从 0 开始</strong> 计数）以及两个整数 <code>target</code> 和 <code>start</code> ，请你找出一个下标 <code>i</code> ，满足 <code>nums[i] == target</code> 且 <code>abs(i - start)</code> <strong>最小化</strong> 。注意：<code>abs(x)</code> 表示 <code>x</code> 的绝对值。</p>\n",
    "\n",
    "<p>返回 <code>abs(i - start)</code> 。</p>\n",
    "\n",
    "<p>题目数据保证 <code>target</code> 存在于 <code>nums</code> 中。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1,2,3,4,5], target = 5, start = 3\n",
    "<strong>输出：</strong>1\n",
    "<strong>解释：</strong>nums[4] = 5 是唯一一个等于 target 的值，所以答案是 abs(4 - 3) = 1 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1], target = 1, start = 0\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>nums[0] = 1 是唯一一个等于 target 的值，所以答案是 abs(0 - 0) = 0 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1,1,1,1,1,1,1,1,1,1], target = 1, start = 0\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>nums 中的每个值都是 1 ，但 nums[0] 使 abs(i - start) 的结果得以最小化，所以答案是 abs(0 - 0) = 0 。\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= nums.length <= 1000</code></li>\n",
    "\t<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>\n",
    "\t<li><code>0 <= start < nums.length</code></li>\n",
    "\t<li><code>target</code> 存在于 <code>nums</code> 中</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [minimum-distance-to-the-target-element](https://leetcode.cn/problems/minimum-distance-to-the-target-element/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [minimum-distance-to-the-target-element](https://leetcode.cn/problems/minimum-distance-to-the-target-element/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,3,4,5]\\n5\\n3', '[1]\\n1\\n0', '[1,1,1,1,1,1,1,1,1,1]\\n1\\n0']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        ans = len(nums)\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                ans = min(abs(i-start),ans)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        lis = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                lis.append(abs(i - start))\n",
    "        return min(lis)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        ans = float('inf')\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target and abs(i-start) < abs(ans - start):\n",
    "                ans = i \n",
    "        return abs(ans-start)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        ans = float(\"inf\")\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]==target:\n",
    "                ans = min(ans,abs(i-start))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def getMinDistance(self,nums: List[int], target: int, start: int) -> int:\n",
    "        res = len(nums)\n",
    "        for i,num in enumerate(nums):\n",
    "            if num == target:\n",
    "                res = min(res,abs(i - start))\n",
    "        return res\n",
    "\n",
    "\n",
    "# 作者：力扣官方题解\n",
    "# 链接：https://leetcode.cn/problems/minimum-distance-to-the-target-element/solutions/755634/dao-mu-biao-yuan-su-de-zui-xiao-ju-chi-b-v4ce/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        min_dist = len(nums)\n",
    "        for i, n in enumerate(nums):\n",
    "            if n == target:\n",
    "                min_dist = min(min_dist, abs(i - start))\n",
    "        return min_dist\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        res=max(nums)\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                ress=abs(i-start) \n",
    "                if res>ress:res=ress \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        l,r = start,start\n",
    "        while True:\n",
    "            if nums[l] == target:\n",
    "                return start-l\n",
    "            elif nums[r] == target:\n",
    "                return r-start\n",
    "            if l > 0:\n",
    "                l -= 1\n",
    "            if r < len(nums)-1:\n",
    "                r += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        res = len(nums)\n",
    "        for i,num in enumerate(nums):\n",
    "            if num == target:\n",
    "                res = min(res,abs(i-start))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        ans=1000\n",
    "        for i in range(len(nums)-start):\n",
    "            if nums[i+start]==target:\n",
    "                ans=i\n",
    "                break\n",
    "        for i in range(start+1):\n",
    "            if nums[-i+start]==target:\n",
    "                ans=min(i,ans)\n",
    "                break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        if nums[start] == target:\n",
    "            return 0\n",
    "        l = nums.__len__()\n",
    "        minimum = l\n",
    "        left, right = start, start\n",
    "        lflag, rflag = False, False\n",
    "        while left >= 0:\n",
    "            if nums[left] == target:\n",
    "                lflag = True\n",
    "                break\n",
    "            left -= 1\n",
    "        while right < l:\n",
    "            if nums[right] == target:\n",
    "                rflag = True\n",
    "                break\n",
    "            right += 1\n",
    "        if lflag and rflag:\n",
    "            return min(abs(start-left), abs(right-start))\n",
    "        elif lflag == False and rflag:\n",
    "            return abs(right-start)\n",
    "        elif lflag and rflag == False:\n",
    "            return abs(start - left)\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        rl=nums.index(target)\n",
    "        rr=len(nums)-nums[::-1].index(target)-1\n",
    "        #print(rl,rr)\n",
    "        minabs=math.inf\n",
    "        for i in range(rl,rr+1):\n",
    "            if nums[i]==target:\n",
    "                minabs=min(minabs,abs(i-start))\n",
    "        return minabs\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        distances = []\n",
    "        for i, n in enumerate(nums):\n",
    "            if n == target:\n",
    "                distances.append(abs(i - start))\n",
    "        return min(distances)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        min_diff = math.inf\n",
    "        n = len(nums)\n",
    "\n",
    "        for i in range(n):\n",
    "            if nums[i] == target:\n",
    "                min_diff = min(min_diff, abs(i - start))\n",
    "        return min_diff\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "\n",
    "        res = 9999999\n",
    "        for idx, ele in enumerate(nums):\n",
    "            if ele == target:\n",
    "                res = min(res,abs(idx-start))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        result = 1000\n",
    "        while target in nums:\n",
    "            indexTarget = nums.index(target)\n",
    "            result = min([result, abs(indexTarget - start)])\n",
    "            nums[indexTarget] = target - 1\n",
    "\n",
    "            if result == 0:\n",
    "                break\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        a=len(nums)\n",
    "        for i in range(start,a):\n",
    "            if nums[i]==target:break\n",
    "        else:i*=2\n",
    "        for j in range(start,-1,-1):\n",
    "            if nums[j]==target:break\n",
    "        else:\n",
    "            j=-1023\n",
    "\n",
    "        return min(i-start,start-j)\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        data=[]\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]==target:\n",
    "                data.append(abs(i-start)) \n",
    "        return min(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        result = 9999\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                if abs(i - start) < result:\n",
    "                    result = abs(i - start)\n",
    "\n",
    "\n",
    "        return result\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        ans = inf\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                ans = min(ans, abs(i-start))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import sys\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        min = sys.maxsize\n",
    "        for i, num in enumerate(nums):\n",
    "            if num == target:\n",
    "                if abs(i - start) < min:\n",
    "                    min = abs(i - start)\n",
    "        return min"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        i=start\n",
    "        j = start\n",
    "        while i>=0 and j<len(nums):\n",
    "            if nums[i] == target or nums[j] == target:\n",
    "                return abs(i-start)\n",
    "            i-=1\n",
    "            j+=1\n",
    "        if i <0:\n",
    "            for s in range(j, len(nums)):\n",
    "                if nums[s] == target:\n",
    "                    return abs(s-start)\n",
    "        else:\n",
    "            for s in range(i, -1, -1):\n",
    "                if nums[s] == target:\n",
    "                    return abs(s-start)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        res = float('inf')\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]==target:\n",
    "                res = min(res, abs(i-start))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        left = 0\n",
    "        right = len(nums) - 1\n",
    "        distence = right\n",
    "        while left <= right:\n",
    "            if nums[left] == target:\n",
    "                distence = min(distence,abs(left - start))\n",
    "            if nums[right] == target:\n",
    "                distence = min(distence, abs(right - start))\n",
    "            left += 1\n",
    "            right -= 1\n",
    "        return distence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        left=right=start\n",
    "        while 1:\n",
    "            if left>=0 and nums[left]==target:\n",
    "                return start-left\n",
    "            if right<len(nums) and nums[right]==target:\n",
    "                return right-start\n",
    "            left-=1\n",
    "            right+=1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        a = []\n",
    "        ans = 1e10\n",
    "        for i,num in enumerate(nums):\n",
    "            if num == target:\n",
    "                a.append(i)\n",
    "        for n in a:\n",
    "            ans = min(ans,abs(n-start))\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        ans = 10001\n",
    "        for i, n in enumerate(nums):\n",
    "            if n == target:\n",
    "                ans = min(ans, abs(i - start))\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        return min(abs(i-start) for i in range(len(nums)) if nums[i]==target)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        left = 0\n",
    "        right = len(nums) - 1\n",
    "        distence = right\n",
    "        while left <= right:\n",
    "            if nums[left] == target:\n",
    "                distence = min(distence,abs(left - start))\n",
    "            if nums[right] == target:\n",
    "                distence = min(distence, abs(right - start))\n",
    "            left += 1\n",
    "            right -= 1\n",
    "        return distence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        i=start\n",
    "        j=start\n",
    "        a=1001\n",
    "        for i in range(i,-1,-1):\n",
    "            if nums[i]==target:\n",
    "                a=min(a,start-i)\n",
    "                break\n",
    "            i-=1\n",
    "        for j in range(j,len(nums)):\n",
    "            if nums[j]==target:\n",
    "                a=min(a,j-start)\n",
    "                break\n",
    "            j+=1\n",
    "        return(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        ans = inf\n",
    "        for i, x in enumerate(nums):\n",
    "            if nums[i] == target:\n",
    "                ans = min(ans, abs(i - start))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        \n",
    "        res = 100000\n",
    "        for idx, num in enumerate(nums):\n",
    "            if num == target:\n",
    "                res = min(res, abs(idx-start))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        mint = math.inf\n",
    "        for i,ele in enumerate(nums):\n",
    "            if (ele == target):\n",
    "                if (abs(i-start)<mint):\n",
    "                    mint = abs(i-start)\n",
    "        return mint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        i, j = start, start\n",
    "        while (nums[i]!=target and nums[j]!=target):\n",
    "            if i>0:\n",
    "                i -= 1\n",
    "            if j < len(nums)-1:\n",
    "                j += 1\n",
    "        if nums[i] == target:\n",
    "            return start - i \n",
    "        if nums[j] == target:\n",
    "            return j - start\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        res = len(nums)\n",
    "        for i, num in enumerate(nums):\n",
    "            if num == target:\n",
    "                res = min(res,abs(i-start))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        n = len(nums)\n",
    "        ans = n\n",
    "        # 正向\n",
    "        for i in range(start, n):\n",
    "            if nums[i] == target:\n",
    "                ans = i - start\n",
    "                break\n",
    "        # 反向\n",
    "        for j in range(start, -1, -1):\n",
    "            if nums[j] == target:\n",
    "                ans = min(ans, start-j)\n",
    "                break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        res = len(nums)\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                res = min(res, abs(i-start))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        ans = float(\"INF\")\n",
    "        for i, n in enumerate(nums):\n",
    "            if n == target:\n",
    "                ans = min(ans, abs(i - start))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        n = len(nums)\n",
    "\n",
    "        front, bck = start, start\n",
    "        while front > - 1 or bck < n:\n",
    "            if front > - 1 and nums[front] == target:\n",
    "                return start - front\n",
    "            if bck < n and nums[bck] == target:\n",
    "                return bck - start\n",
    "            \n",
    "\n",
    "            front -= 1\n",
    "            bck += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        mint = math.inf\n",
    "        for i,ele in enumerate(nums):\n",
    "            if (ele == target):\n",
    "                if (abs(i-start)<mint):\n",
    "                    mint = abs(i-start)\n",
    "        return mint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        count = {}\n",
    "        ans = inf\n",
    "        for i, num in enumerate(nums):\n",
    "            if num == target:\n",
    "                #count[num] = abs(i - start)\n",
    "                ans = min(ans, abs(i - start))\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        maxInt = sys.maxsize\n",
    "        for i in range(0,len(nums)):\n",
    "            if nums[i] == target:\n",
    "                res = abs(i - start)\n",
    "                maxInt = min(maxInt,res)\n",
    "        return maxInt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        min_diff = math.inf\n",
    "        n = len(nums)\n",
    "\n",
    "        for i,v in enumerate(nums):\n",
    "            if v == target:\n",
    "                min_diff = min(min_diff, abs(i - start))\n",
    "        return min_diff\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        res = len(nums)\n",
    "        for i, num in enumerate(nums):\n",
    "            if num == target:\n",
    "                res = min(res, abs(i - start))\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        i, j = start, start\n",
    "        while(i >= 0 or j < len(nums)):\n",
    "            if i >= 0 and nums[i] == target:\n",
    "                return start - i\n",
    "            if j < len(nums) and nums[j] == target:\n",
    "                return j - start\n",
    "            j += 1\n",
    "            i -= 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        ans = float('inf')\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target and abs(i-start) < abs(ans - start):\n",
    "                ans = i \n",
    "        return abs(ans-start)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        res = 10000\n",
    "        for i in range(len(nums)):\n",
    "            if (nums[i] == target) & (res > abs(i - start)):\n",
    "                res = abs(i - start)\n",
    "        return res\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        min_distance = len(nums)\n",
    "        for i, num in enumerate(nums):\n",
    "            if num == target:\n",
    "                abs_distance = abs(i - start) \n",
    "                min_distance = min(min_distance, abs_distance)\n",
    "        return min_distance\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        res = 1000\n",
    "        for i, num in enumerate(nums):\n",
    "            if num == target:\n",
    "                res = min(res, abs(start - i))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        ans = [index for index, value in enumerate(nums) if value == target]\n",
    "        return min(list(map(lambda x: abs(x-start), ans)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\r\n",
    "        right = float('inf')\r\n",
    "        for i in range(start, len(nums)):\r\n",
    "            if nums[i] == target:\r\n",
    "                right = i\r\n",
    "                break\r\n",
    "        left = -float('inf')\r\n",
    "        for i in range(start, -1, -1):\r\n",
    "            if nums[i] == target:\r\n",
    "                left = i\r\n",
    "                break\r\n",
    "\r\n",
    "        return min(start - left, right - start)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        ans=1050\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]==target:\n",
    "                if abs(i-start)<ans:\n",
    "                    ans=abs(i-start)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        result = len(nums) + 1\n",
    "\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                result = min(abs(i-start), result)\n",
    "        \n",
    "        return result\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        ans=len(nums)\n",
    "        if len(nums)==1:\n",
    "            return 0\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]==target:\n",
    "                ans=min(abs(i-start),ans)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        left = 0\n",
    "        right = len(nums) - 1\n",
    "        distence = right\n",
    "        while left <= right:\n",
    "            if nums[left] == target:\n",
    "                distence = min(distence,abs(left - start))\n",
    "            if nums[right] == target:\n",
    "                distence = min(distence, abs(right - start))\n",
    "            if distence == 0:\n",
    "                return 0\n",
    "            left += 1\n",
    "            right -= 1\n",
    "        return distence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        res = len(nums)\n",
    "        for i, num in enumerate(nums):\n",
    "            if num == target:\n",
    "                res = min(res, abs(i - start))\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        l = start - 1\n",
    "        r = start\n",
    "        minlen = 1000\n",
    "        while l>=0:\n",
    "            if nums[l] == target:\n",
    "                minlen = min(minlen, abs(l-start))\n",
    "                break\n",
    "            l -= 1\n",
    "        while r<=len(nums)-1:\n",
    "            if nums[r] == target:\n",
    "                minlen = min(minlen, abs(r-start))\n",
    "                break\n",
    "            r += 1\n",
    "        return minlen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        lnum=[]\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                lnum.append(i)\n",
    "        res=[abs(i-start )for i in lnum]\n",
    "        return min(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        left = 0\n",
    "        right = len(nums) - 1\n",
    "        distence = right\n",
    "        while left <= right:\n",
    "            if nums[left] == target:\n",
    "                distence = min(distence,abs(left - start))\n",
    "            if nums[right] == target:\n",
    "                distence = min(distence, abs(right - start))\n",
    "            left += 1\n",
    "            right -= 1\n",
    "        return distence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        left = 0\n",
    "        right = len(nums) - 1\n",
    "        distence = right\n",
    "        while left <= right:\n",
    "            if nums[left] == target:\n",
    "                distence = min(distence,abs(left - start))\n",
    "            if nums[right] == target:\n",
    "                distence = min(distence, abs(right - start))\n",
    "            left += 1\n",
    "            right -= 1\n",
    "        return distence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        left = 0\n",
    "        right = len(nums) - 1\n",
    "        distence = right\n",
    "        while left <= right:\n",
    "            if nums[left] == target:\n",
    "                distence = min(distence,abs(left - start))\n",
    "            if nums[right] == target:\n",
    "                distence = min(distence, abs(right - start))\n",
    "            left += 1\n",
    "            right -= 1\n",
    "        return distence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        left = 0\n",
    "        right = len(nums) - 1\n",
    "        distence = right\n",
    "        while left <= right:\n",
    "            if nums[left] == target:\n",
    "                distence = min(distence,abs(left - start))\n",
    "            if nums[right] == target:\n",
    "                distence = min(distence, abs(right - start))\n",
    "            if distence == 0:\n",
    "                return 0\n",
    "            left += 1\n",
    "            right -= 1\n",
    "        return distence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        res = len(nums)\n",
    "        for i, num in enumerate(nums):\n",
    "            if num == target:\n",
    "                res = min(res, abs(i - start))\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        n = len(nums)\n",
    "\n",
    "        front, bck = start, start\n",
    "        while front > - 1 or bck < n:\n",
    "            if front > - 1 and nums[front] == target:\n",
    "                return start - front\n",
    "            if bck < n and nums[bck] == target:\n",
    "                return bck - start\n",
    "            \n",
    "\n",
    "            front -= 1\n",
    "            bck += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:\n",
    "        n = len(nums)\n",
    "\n",
    "        front, bck = start, start\n",
    "        while front > - 1 or bck < n:\n",
    "            if front > - 1 and nums[front] == target:\n",
    "                return start - front\n",
    "            if bck < n and nums[bck] == target:\n",
    "                return bck - start\n",
    "            \n",
    "\n",
    "            front -= 1\n",
    "            bck += 1"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
